package com.cuitrip.business.login.proxy;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

import com.cuitrip.business.login.model.ThirdAccount;
import com.lab.app.BaseActivity;
import com.lab.logtrack.AppLog;
import com.lab.widget.dialog.LoadingDialog;
import com.mob.tools.utils.UIHandler;

import java.lang.ref.SoftReference;
import java.util.HashMap;

import cn.sharesdk.facebook.Facebook;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.google.GooglePlus;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.wechat.friends.Wechat;

/**
 * 第三方帐号授权
 * Created by ak on 15/9/23.
 */
public class ThirdAccountProxy implements Handler.Callback,
        PlatformActionListener {

    private static final int MSG_USERID_FOUND = 1;
    private static final int MSG_LOGIN = 2;
    private static final int MSG_AUTH_CANCEL = 3;
    private static final int MSG_AUTH_ERROR = 4;
    private static final int MSG_AUTH_COMPLETE = 5;
    private OnAuthListener onAuthListener;
    private int accountType;
    private LoadingDialog sweetAlertDialog;

    private Handler handler = new Handler();

    private SoftReference<BaseActivity> mActivityReference;

    public static boolean isQQInstalled(Context context) {
        QQ plat = new QQ(context);
        return plat.isClientValid();
    }

    public static boolean isWechatInstall(Context context) {
        Wechat plat = new Wechat(context);
        return plat.isClientValid();
    }

    public static boolean isGoogleInstall(Context context) {
        GooglePlus plat = new GooglePlus(context);
        return plat.isClientValid();
    }

    public static boolean isWeiboInstall(Context context) {
        SinaWeibo plat = new SinaWeibo(context);
        return plat.isClientValid();
    }

    public static boolean isFacebookInstall(Context context) {
        Facebook plat = new Facebook(context);
        return plat.isClientValid();
    }

    public void init(BaseActivity activity) {
        mActivityReference = new SoftReference<BaseActivity>(activity);
        ShareSDK.initSDK(mActivityReference.get());
        sweetAlertDialog = LoadingDialog.getInstance();
    }

    private BaseActivity getActivity() {
        if (mActivityReference.get() != null && mActivityReference.get() != null) {
            return mActivityReference.get();
        }
        return null;
    }

    public void destroy() {
        ShareSDK.stopSDK();
    }

    public void authorizeThirdAccount(int type, OnAuthListener onAuthListener) {
        this.accountType = type;
        this.onAuthListener = onAuthListener;
        if (getActivity() == null) {
            return;
        }
        switch (type) {
            case ThirdAccount.WE_CHAT:
                wechatLogin(mActivityReference.get());
                break;
            case ThirdAccount.WEIBO:
                weiboLogin(mActivityReference.get());
                break;
            case ThirdAccount.FACEBOOK:
                facebookLogin(mActivityReference.get());
                break;
            case ThirdAccount.GOOGLE:
                googleLogin(mActivityReference.get());
                break;
        }
        getActivity().showDialogFragment(sweetAlertDialog);
    }

    private void wechatLogin(Context context) {
        Wechat wechat = new Wechat(context);
        authorize(wechat, true);
    }

    private void weiboLogin(Context context) {
        SinaWeibo weibo = new SinaWeibo(context);
        authorize(weibo, isWeiboInstall(context) ? false : true);
    }

    private void facebookLogin(Context context) {
        Facebook facebook = new Facebook(context);
        authorize(facebook, false);
    }

    private void googleLogin(Context context) {
        GooglePlus googlePlus = new GooglePlus(context);
        authorize(googlePlus, true);
    }

    private void authorize(Platform plat, boolean isSSOSetting) {
        plat.removeAccount(true);
        plat.setPlatformActionListener(this);
        plat.SSOSetting(isSSOSetting);
        if (accountType == ThirdAccount.GOOGLE) {
            plat.authorize();
        } else {
            plat.showUser(null);
        }

    }

    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case MSG_AUTH_CANCEL: {
                if (onAuthListener != null) {
                    onAuthListener.onCancel();
                }
            }
            break;
            case MSG_AUTH_ERROR: {
                if (onAuthListener != null) {
                    onAuthListener.onCancel();
                }
            }
            break;
            case MSG_AUTH_COMPLETE: {
                if (onAuthListener != null) {
                    ThirdAccount thirdAccount = (ThirdAccount) msg.obj;
                    onAuthListener.onComplete(thirdAccount);
                }
            }
            break;
        }
        return false;
    }

    //在oncomplete中的hashmap返回用户信息
    @Override
    public void onComplete(Platform platform, int action, HashMap<String, Object> hashMap) {

        if (action == Platform.ACTION_USER_INFOR) {
            Message msg = new Message();
            msg.what = MSG_AUTH_COMPLETE;
            ThirdAccount thirdAccount = new ThirdAccount();
            thirdAccount.setAccountType(accountType);
            thirdAccount.setDataParams(hashMap);
            msg.obj = thirdAccount;
            UIHandler.sendMessage(msg, this);
        }
        dismissDialog();
    }

    @Override
    public void onError(Platform platform, int action, Throwable throwable) {
        if (action == Platform.ACTION_USER_INFOR) {
            AppLog.e(throwable.toString());
            UIHandler.sendEmptyMessage(MSG_AUTH_ERROR, this);

        }
        dismissDialog();

    }

    private void dismissDialog() {
        handler.post(new Runnable() {
            @Override
            public void run() {
                if (getActivity() != null) {
                    getActivity().hideDialogFragment(sweetAlertDialog);
                }
            }
        });
    }

    @Override
    public void onCancel(Platform platform, int action) {
        if (action == Platform.ACTION_USER_INFOR) {
            UIHandler.sendEmptyMessage(MSG_AUTH_CANCEL, this);
        }
        dismissDialog();
    }

    public void onActivityStop() {
        dismissDialog();
    }

    public interface OnAuthListener {

        public void onComplete(ThirdAccount thirdAccount);

        public void onCancel();
    }

}
